/**
 *  会员积分详情
 */
define(function(require,exports,module) {
    var myPub = require('./public');

    var oLoadingBtn = $('.js_loadingBtn');      // 加载提示
    var oLoadMore = $('.js_loadMore');          // 加载更多
    var oLoadNone = $('.js_loadNone');          // 没有了
    var oLoadIng = $('.js_loadIng');            // 加载中
    var off_tap = true;
    var off_tapTime = null;

    var oIncomeList = $('.js_incomeList');      // 积分数据区域
    var sClickJson = null;
    var goodsDateUrl = '/myInfo/getMemberPointRecord';

    var oChoujiang = $('.js_choujiang');
    var remindStyle = "background-color:rgba(0,0,0,0.5); color:#fff; border:none; border-radius: 5px; min-width:50%"

    var oJumpOrderList = $('.js_jumpOrderList');            // 跳转订单链接
    var oIntegralShopHref = $('.js_integralShopHref');      // 积分商城跳转

    module.exports = {
        init: function(data,names) {
            ajaxData.ListAjaxData();


            // 检测app中跳转 积分商城链接和订单
            myPub.JudgeAppHide(function () {
                // ios
                // 订单跳转
                oJumpOrderList.attr('href', 'sf://orderPageVC');
                // 积分商城跳转
                oIntegralShopHref.attr('href', 'sf://pointsMallVC');
            }, function () {
                // android
                // 订单跳转
                AndroidHerf(oJumpOrderList, function () {
                    android.toMyOrderActivity();
                });

                // 积分商城跳转
                AndroidHerf(oIntegralShopHref, function () {
                    android.toJiFenStore();
                });

            }, function () {
                // h5
                oJumpOrderList.attr('href', oJumpOrderList.attr('_href'));
                oIntegralShopHref.attr('href', oIntegralShopHref.attr('_href'));
            });
        }
    };

    oChoujiang.click(function () {
        myPub.TipLayer('敬请期待！',remindStyle);
    });

    // 点击加载
    oLoadMore.hammer().on('tap', function () {
        // 防止多次点击
        if(off_tap) {
            off_tap = false;
            // 加载中显示
            oLoadingBtn.hide();
            oLoadIng.show();
            // 加入定时器 800毫秒
            clearTimeout(off_tapTime);
            off_tapTime = setTimeout(function () {
                // 数据写入前判断 当前页数和总页数
                if(ajaxData.pagearr < ajaxData.pageCout) {
                    ajaxData.start = ajaxData.limit * ajaxData.pagearr;
                    ajaxData.end = ajaxData.start + ajaxData.limit;

                    ajaxData.pagearr++;
                    sClickJson = {
                        'start' : ajaxData.start,
                        'end' :  ajaxData.end
                    },

                    ajaxData.ListHtml(ajaxData.getData, sClickJson, function () {
                        // 数据写入后再次判断
                        if(ajaxData.pagearr < ajaxData.pageCout) {
                            oLoadingBtn.hide();
                            oLoadMore.show();
                        }
                        else {
                            ajaxData.pagearr = ajaxData.pageCout;
                            oLoadingBtn.hide();
                            oLoadNone.show();
                        }
                    });
                }
                else {
                    oLoadingBtn.hide();
                    oLoadNone.show();
                }
                off_tap = true;
            }, 800)
        }
    });
    

    var ajaxData = {
        limit: 10,                       // 每次加载多少数据
        getData: [],                    // 数据记录
        pagearr: 1,                     // 当前页码
        pageCout: 0,                    // 页码总数
        start:0,                        // 开始数据
        end: 0,                         // 结束数据

        // 设置商品数据
        ListAjaxData: function (bOff,fn) {
            G_JSON_POST(goodsDateUrl,{
                // "maxLimit":maxLimit
            },function (data) {
                // 存储数据
                ajaxData.getData = data.obj;
                // 设置第一次结束数据长度
                ajaxData.end = ajaxData.limit;

                // 填写配置 开始和结束数据
                var options = {
                    'start' : ajaxData.start,
                    'end' :  ajaxData.end,
                };

                // 获取总页数
                ajaxData.pageCout = Math.ceil(ajaxData.getData.length / ajaxData.limit);

                if( data.status === "success") {

                    ajaxData.ListHtml(ajaxData.getData, options);

                    data = null;
                    if(fn)fn()
                }

            }, function () {
                // start
            }, function () {
                // end
            });

        },

        ListHtml: function (data, options, fn) {
            var shtml = '';
            var _start = options.start;
            var _end = options.end;
            if(_end >= ajaxData.getData.length) {
                _end = ajaxData.getData.length;
            }

            var times = [];
            var sHtml;
            for (var i=_start; i< _end; i++) {
                times[i] = myPub.FormatDateTime(new Date(data[i].createDate),'minSec');
                var op = data[i].operation;
                var ops = checkOp(op);
                var orderCheck = checkOrderSn(data[i].orderSn);
                if(data[i].detail>0){
                    sHtml = '<td><span class="integralNum">+'+ data[i].detail +'</span></td>';
                }else{
                    sHtml = '<td><span class="integralNum">'+ data[i].detail +'</span></td>';
                }
                shtml +='<tr>'+
                        '    <td>'+
                        '        <span>'+ops+'</span>'+
                        orderCheck+
                        '    </td>'+
                        sHtml+
                        '    <td><em>'+ times[i] +'</em></td>'+
                        '</tr>'
            }
            var oShtml = $(shtml);
            oIncomeList.find('.js_incomeList_detail').append(oShtml);

            if(fn)fn();
        }

    }

    function checkOrderSn(orderSn) {
        if(orderSn == null ||orderSn == 'undefined'){
            return "";
        }else{
            return '<em>'+orderSn+'</em>';
        }
    }

    function checkOp(op) {
        if(op == "PARTAFTERSALE" || op == "SHOPING" || op == "SHOPUSE"||op == "FIRSTSHOPING"||op == "SHOPDEDUCT"){
            return "购物";
        }else if(op == "LOGIN"){
            return　"每日签到";
        }else if(op == "SYSTEMSENDING"){
            return "系统发放";
        }else if(op == "TIAOZENG"){
            return "手动调增";
        }else if(op == "TIAOJIAN"){
            return "手动调减";
        }else if(op == "NOPAY"){
            return "未支付退还";
        }else if(op == "COMMENT"){
            return "评论";
        }else if(op == "COMMENTREWARD"){
            return "评论奖励";
        }else if(op == "COMPLETEINFORMATION" || op == "BINGMOBILE"){
            return "完善资料";
        }else if(op == "AFTERSALE" ||op == "PARTAFTERSALE" || op == "NOPAY"){
            return "退款";
        }else{
            return "其他";
        }
    }

    // 安卓绑定点击事件
    function AndroidHerf(obj, fn) {
        obj.off('click');
        obj.on('click',function() {
            if(fn)fn();
        })
    }

});